
import {showToast} from "../../utils/tool";
import {getLoginUser, setLoginToken} from "../../service/user";

const global = getApp().globalData

Component({
    options:{
        styleIsolation: 'apply-shared'
    },
    properties: {},
    data: {
        showWxMpLogin: true,
        imageUrl:global.imageUrl
    },
    methods: {
        /**
         * 登录成功后的功能逻辑
         */
        loginSuccess(res){
            console.log("loginRes",res)
            const {token} = res.detail

            // 将登录标记存入缓存，以下次访问时自动登录
            setLoginToken(token)
            // 获取用户信息
            getLoginUser()

            showToast("登录成功", {
                'icon':'success',
                timeout: () => {

                    const pages = getCurrentPages()
                    const currPage = pages[0]
                    const options = currPage.options

                    if(options.redirect){
                        wx.redirectTo({
                            url:options.redirect
                        })
                    }else if(pages.length > 1){ // 返回上一页
                        const prevPage = pages[pages.length - 2];
                        prevPage.onLoad(prevPage.options)
                        wx.navigateBack({
                            delta: 1,
                        })
                    }else{  // 去首页
                        wx.reLaunch({
                            url: '/pages/index/index'
                        })
                    }
                }
            })
        },
        loginTypeChange(){
            this.setData({
                showWxMpLogin: !this.data.showWxMpLogin
            })
        }
    }
});
